package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.pojo.*;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.mysql.jdbc.Wrapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {


    @Autowired
    private ApArticleMapper apArticleMapper;
    private final static short MAX_PAGE_SIZE=50;

    /**
     * 根据参数加载文章列表
     *
     * @param loadtype 1 加载更多 2加载最新
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {
        //根据这个手写的sql去进行联查,查出的数据返回给前端接口.接下来需要做参数的校验操作
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, loadtype);
        //参数的校验
        //分页参数的校验
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            size = 10;
        }
      //分页的值不超过50
        size=Math.min(size,MAX_PAGE_SIZE);
        //这个地方的判断是,操作不是刷新
        if(!loadtype.equals(ArticleConstants.LOADTYPE_LOAD_MORE)&& !loadtype.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){
            loadtype=ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //频道的校验
        if (StringUtils.isBlank(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        //时间的判断
        if (dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
        return ResponseResult.okResult(apArticles);
    }

    /**
     * App文章保存方法
     *
     * @param dto
     * @return
     */
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Override
    public ResponseResult save(ApArticleDto dto) {
        //参数判断
        if (dto==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        //2.判断id是否存在
        if (dto.getId()==null){
            //2.1不存在的话,保存文章  文章配置   文章内容
            //保存文章
            save(apArticle);

            //保存文章的配置
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);
            //保存文章的内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        }else{
            //2.2存在,保存文章内容  文章
            updateById(apArticle);

            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId()));
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }
        //3.返回
        return ResponseResult.okResult(apArticle.getId());
    }
}
